package com.vv.edu.info.manager.controller;

import com.vv.edu.info.manager.domain.StudentDomain;
import com.vv.edu.info.manager.domain.Teacher;
import com.vv.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {

    private final TeacherService teacherService = new TeacherService();
    private final Scanner sc = new Scanner(System.in);
    public void start() {
        teacherLoop: while(true){
            System.out.println("=========欢迎来到<老师>管理系统=========");
            System.out.println("请输入您的选择：1.添加老师 2.删除老师 3.修改老师 4.查询老师 5.退出");
            String choice = sc.next();
            switch (choice){
                case "1":
                    // System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    // System.out.println("删除老师");
                    deleteTeacherById();
                    break;
                case "3":
                    // System.out.println("修改老师");
                    updataTeacher();
                    break;
                case "4":
                    // System.out.println("查询老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("欢迎您使用老师管理系统，再见！");
                    break teacherLoop;
                default:
                    System.out.println("您输入的有误，请重新输入！");
            }
        }
    }

    private void updataTeacher() {
        String id;
        while(true){
            System.out.println("请输入需要修改的教师工号：");
            id = sc.next();
            if(id.equals("-1")){
                System.out.println("修改失败！");
                return;
            }
            boolean exists = teacherService.isExists(id);
            if(!exists){
                System.out.println("您输入的工号不存在，请重新输入(或按-1结束修改操作)：");
            }else{
                break;
            }
        }
        Teacher teacher = inputTeacherInfo(id);
        teacherService.updataTeacher(id, teacher);
        System.out.println("修改成功！");
    }

    public void deleteTeacherById() {
        String id;
        while(true){
            System.out.println("请输入需要删除的教师工号：");
            id = sc.next();
            if(id.equals("-1")){
                System.out.println("删除失败！");
                return;
            }
            boolean exists = teacherService.isExists(id);
            if(!exists){
                System.out.println("您输入的工号不存在，请重新输入(或按-1结束删除操作)：");
            }else{
                teacherService.deleteTeacher(id);
                System.out.println("删除成功！");
                break;
            }
        }
    }

    public void findAllTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        if(teachers == null) {
            System.out.println("查无信息，请添加信息！");
            return;
        }
        // 遍历数组，获取学生信息并打印
        System.out.println("工号\t\t姓名\t年龄\t生日");
        for (Teacher teacher : teachers) {
            if (teacher != null) {
                System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "\t\t" + teacher.getBirthday());
            }
        }
    }

    public void addTeacher() {
        String id;
        while(true){
            System.out.println("请输入老师工号:");
            id = sc.next();
            boolean flag = teacherService.isExists(id);
            if(flag){
                System.out.println("工号已被占用，请重新输入：");
            }else {
                break;
            }
        }
        Teacher teacher = inputTeacherInfo(id);
        // 3. 将老师对象传递给teacherService(业务员)中的addTeacher方法
        boolean result = teacherService.addTeacher(teacher);
        // 4. 根据返回的boolean类型的结果，再控制台打印成功or失败
        if(result){
            System.out.println("添加成功！");
        }else{
            System.out.println("添加失败！");
        }
    }

    // 录入老师信息并返回对象
    public Teacher inputTeacherInfo(String id){
        System.out.println("请输入老师姓名:");
        String name = sc.next();
        System.out.println("请输入老师年龄:");
        String age = sc.next();
        System.out.println("请输入老师生日:");
        String birthday = sc.next();
        // 2. 将学生信息封装为老师对象
        return new Teacher(id, name, age, birthday);
    }
}
